<template>
  <div class="postbody">
    <div class="post_con">
      <div class="post_header">
        <p>贴子发布</p>
        <button class="fbbut" @click="post_fb"><i class="iconfont icon-fabusekuai icon" />发布</button>
      </div>
      <el-divider />
      <div class="post_tit">
        发布类型标签
        <div class="post_bq" id="post_bq">
          <div class="active" @click="post_km('科目一',$event)">#科目一</div>
          <div @click="post_km('科目二',$event)">#科目二</div>
          <div @click="post_km('科目三',$event)">#科目三</div>
          <div @click="post_km('科目四',$event)">#科目四</div>
        </div>
      </div>
      <div class="post_tit">
        标题
        <el-input v-model="posttitle" placeholder="我是标题" style="margin-left: 10px" clearable />
      </div>
      <div class="post_c">
        内容
        <el-input
            v-model="textarea"
            :rows="2"
            type="textarea"
            placeholder="我是内容"
            style="margin-left: 10px"
        />
      </div>
    </div>





    <div class="menu-box" v-if="LoginI">
      <!-- 图标按钮 -->
      <div class="menu-button" @click="ClickDHL">
        <div class="line-box">
          <div class="line"></div>
          <div class="line"></div>
          <div class="line"></div>
        </div>
      </div>
      <!-- 菜单列表 -->
      <ul class="menu-list">
        <li @click="myPost"><i class="iconfont icon-wofadetiezi"></i><span>我的帖子</span></li>
        <li @click="toPost"><i class="iconfont icon-huifu"></i><span>发布帖子</span></li>
        <li @click="toHome"><i class="iconfont icon-shouye"></i><span>首页</span></li>
      </ul>
    </div>
  </div>
</template>

<script setup>
import router from "@/router";
import {useStore} from "vuex";
import {ref} from "vue";
import {InsertPost} from "@/api/post";
import {ElMessage} from "element-plus";
const store = useStore()
const LoginI = store.getters.Id

const posttitle = ref('')
const textarea = ref('')
const post_Km = ref('科目一')

// 获取当前时间
const UpData=()=>{
  const nowDate = new Date();
  const date = {
    year: nowDate.getFullYear(),
    month: nowDate.getMonth() + 1,
    date: nowDate.getDate(),
    hour: nowDate.getHours(),
    min: nowDate.getMinutes(),
    sec:nowDate.getSeconds(),
  }
  const newmonth = date.month>10?date.month:'0'+date.month
  const day = date.date>10?date.date:'0'+date.date
  return  date.year + '-' + newmonth + '-' + day + ' ' + date.hour + ':' + date.min + ':' + date.sec
}
const datenow = ref(UpData())

const post_km = (select,event) =>{
  post_Km.value = select
  let postbq = document.getElementById('post_bq').childNodes
  postbq.forEach((item)=>{
    item.classList.remove('active')
  })
  event.target.classList.add('active')
}

//发布帖子
const post_fb = async () =>{
  if (posttitle.value == ''||textarea.value == ''){
    ElMessage.warning('发布内容不能为空')
  }else {
    await InsertPost({
      userId:LoginI,
      title:posttitle.value,
      content:textarea.value,
      date:datenow.value,
      click:0,
      subject:post_Km.value,
      type:store.getters.Type
    }).then(res=>{
      if (res.code === 20011){
        ElMessage.success('发布成功')
        setTimeout(() => {
          router.replace('/fourmm')
        }, 1000);
      }else {
        ElMessage.error('发布失败')
      }
    })
  }

}


//我的帖子
const myPost = () =>{
  router.replace('/mypost')
}
//发布帖子
const toPost = () =>{
  router.replace('/post')
}
//帖子首页
const toHome = () =>{
  router.replace('/fourmm')
}
//下方导航
const ClickDHL = () =>{
  // 要操作的元素
  const menu_box=document.querySelector('.menu-box');
  menu_box.classList.toggle('active');
}
</script>

<style scoped>
/**{*/
/*  border: 1px solid blue;*/
/*}*/
.postbody{
  flex: 1;
  overflow: hidden;
  display: flex;
  position: relative;
  flex-direction: column;
  align-items: center;
  background-color: white;
}
.post_header{
  display: flex;
  justify-content: space-between;
  font-weight: 600;
  font-size: 20px;
  font-family: 华文仿宋, serif;
}
.fbbut{
  width: 100px;
  position: relative;
  background-color: #4da9ff;
  color: white;
  border: 1px solid #4da9ff;
  overflow: hidden;
}
.fbbut:active{
  transform: translate(0,-2px);
}
.icon{
  position: absolute;
  top: 50%;
  left: 0;
  width: 20px;
  transform: translate(0,-50%);
  background-color: inherit;
  transition: width .5s ease;
}
.fbbut:hover .icon{
  width: 100px;
}
.post_con{
  margin: 10px;
  width: 60%;
  height: auto;
  border: 1px solid #dadada;
  box-shadow: #cccccc 0 0 6px;
  border-radius: 12px;
  box-sizing: border-box;
  padding: 10px;
}
.post_tit{
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 20px;
  font-family: 华文仿宋, serif;
  white-space: nowrap;
  margin-bottom: 20px;
}
.post_bq{
  flex:1;
  display: flex;
  margin-left: 10px;
}
.post_bq>div{
  color: #7696b2;
  opacity: 0.6;
  margin-left: 10px;
  cursor: pointer;
  transition: color .2s ease,opacity .2s ease;
}
.post_bq>div:hover{
  opacity: 1;
  color: #0085ff;
}
.post_bq>div.active{
  opacity: 1;
  color: #0082ff;
}
.post_c{
  display: flex;
  font-weight: 600;
  font-size: 20px;
  font-family: 华文仿宋, serif;
  white-space: nowrap;
}


.menu-box{
  /* 固定定位 右下角 */
  position: fixed;
  bottom: 40px;
  right: 40px;
}
/* 菜单按钮 */
.menu-button{
  width: 50px;
  height: 50px;
  background-color: #5c67ff;
  border-radius: 50%;
  /* 投影 */
  box-shadow: 0 0 0 4px rgba(92,103,255,0.3);
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 1;
  cursor: pointer;
  /* 过渡 */
  transition: 0.2s ease-in;
}
.menu-button:hover{
  background-color: #4854ff;
  box-shadow: 0 0 0 8px rgba(92,103,255,0.3);
}
.menu-button .line-box{
  width: 20px;
  height: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  cursor: pointer;
  transition: transform 0.3s ease-out;
}
/* 菜单按钮图标（三条杠） */
.menu-button .line{
  background-color: #fff;
  width: 100%;
  height: 2px;
  border-radius: 2px;
}
/* 前后两条短，中间的长 */
.menu-button .line:first-child{
  width: 50%;
  /* 设置变换的基点 */
  transform-origin: right;
  /* 过渡效果 */
  transition: transform 0.3s ease-in-out;
}
.menu-button .line:last-child{
  width: 50%;
  align-self: flex-end;
  transform-origin: left;
  transition: transform 0.3s ease-in-out;
}
/* 菜单列表 */
.menu-list{
  width: 140px;
  height: 160px;
  background-color: #fff;
  border-radius: 8px;
  list-style: none;
  padding: 6px;
  box-shadow: 0 0 4px 4px rgba(92,103,255,0.15);
  position: absolute;
  right: 15px;
  bottom: 15px;
  /* 默认隐藏 */
  opacity: 0;
  transform: scale(0);
  /* 设置变换的基点 */
  transform-origin: bottom right;
  /* 过渡效果 */
  transition: 0.3s ease;
  /* 过渡延迟 */
  transition-delay: 0.1s;
}
/* 菜单项 */
.menu-list li{
  display: flex;
  align-items: center;
  padding: 10px;
  color: #1c3991;
  cursor: pointer;
  position: relative;
  /* 默认隐藏 */
  opacity: 0;
  transform: translateX(-10px);
  transition: 0.2s ease-in;
}
.menu-list li:hover{
  color: #5c67ff;
}
/* 菜单项下边框 */
.menu-list li::before{
  content: "";
  width: calc(100% - 24px);
  height: 1px;
  background-color: rgba(92,103,255,0.1);
  position: absolute;
  bottom: 0;
  left: 12px;
}
/* 最后一项不用下边框 */
.menu-list li:last-child::before{
  display: none;
}
/* 菜单项图标 */
.menu-list .fa{
  font-size: 18px;
  width: 18px;
  height: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
}
/* 菜单项文本 */
.menu-list span{
  font-size: 14px;
  margin-left: 8px;
}
/* 活动态下的部分样式变化 */
/* 三条杠的变化 */
.active .line-box{
  transform: rotate(-45deg);
}
.active .line-box .line:first-child{
  transform: rotate(-90deg) translateX(1px);
}
.active .line-box .line:last-child{
  transform: rotate(-90deg) translateX(-1px);
}
/* 菜单列表的变化 */
.active .menu-list{
  opacity: 1;
  transform: scale(1);
}
.active .menu-list li{
  /* 执行动画：动画名 时长 线性 停留在最后一帧 */
  animation: fade-in-item 0.4s linear forwards;
}
/* 接下来为每一项设置动画延迟时间 */
.active .menu-list li:nth-child(1){
  animation-delay: 0.1s;
}
.active .menu-list li:nth-child(2){
  animation-delay: 0.2s;
}
.active .menu-list li:nth-child(3){
  animation-delay: 0.3s;
}
.active .menu-list li:nth-child(4){
  animation-delay: 0.4s;
}

/* 定义动画 */
@keyframes fade-in-item {
  100%{
    transform: translateX(0);
    opacity: 1;
  }
}
</style>